/**
 * YouTube Class - Created by FlashFrameLess Team OCT/2008
 *
 * @author		FabioTNT / FlashFrameLessTeam - http://www.flashframeless.com/
 * @version		1.0
 */

package com.flashframeless.webapis.youtube {
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import com.flashframeless.webapis.youtube.YouTubeEvent;
	
	public class YouTube extends EventDispatcher {
		
		private var _loader			: URLLoader;
		private var _videoId		: String;
		private var _fvlUrl			: String;
		private var _xml			: XML;
		private var i				: uint;	
		private var _defaultUser	: String;
		private var _arrVideos		: Array = new Array();
		private namespace _atom		= 'http://www.w3.org/2005/Atom';
		private namespace _media	= 'http://search.yahoo.com/mrss/';
		private const FLV_REGEX		: RegExp = /&t=([^&]*)&hl=/gi;
		
		public function YouTube() {
			
			//
			
		}
		
		public function searchVideosByUser( user:String = '' ):void {
			
			if ( user == '' ) {
				
				user = _defaultUser;
				
			}
			
			var url	: String = 'http://gdata.youtube.com/feeds/api/users/' + user + '/uploads';			
			
			search( url );
			
		}
		
		public function getFLV( id:String ):void {
			
			_loader = new URLLoader();
			_loader.addEventListener( Event.COMPLETE, flvHandler );
			
			_videoId = id;
			_loader.load( new URLRequest( 'http://br.youtube.com/watch?v=' + _videoId ) );
			
		}
		
		function flvHandler( evt:Event ):void {
			
			_loader.removeEventListener( Event.COMPLETE, flvHandler );
			
			var flvURL:String = evt.target.data.toString().match( FLV_REGEX )[0];
			flvURL = flvURL.substring( 3, flvURL.length - 4 );	
			flvURL = "http://www.youtube.com/get_video.php?video_id=" + _videoId + "&t=" + flvURL;
			
			var dispatch:YouTubeEvent = new YouTubeEvent( YouTubeEvent.FLV_COMPLETE );
			dispatch.videoURL = flvURL;
			
			dispatchEvent( dispatch );
			
		}
		
		private function search( url:String ):void {
			
			_loader = new URLLoader();
			
			_loader.addEventListener( Event.COMPLETE, searchComplete );
			_loader.load( new URLRequest( url ) );
			
		}
		
		private function searchComplete( evt:Event ):void {
			
			_loader.removeEventListener( Event.COMPLETE, searchComplete );
			
			//Extract data
			_xml = new XML( evt.target.data );
			ParseData( _xml );
			
		}
		
		private function ParseData( data:XML ):void {
			
			_arrVideos = [];
			
			use namespace _atom; 
			use namespace _media; 
			
			for each( var entry:XML in data..entry ) { 
				
				var group:XML = entry.group[0];
				var thumbs:Array = new Array();
				
				for ( i = 0; i < 4; i++ ) {
					
					thumbs.push( group.thumbnail[i].@url.toString() );
					
				}
				
				_arrVideos.push( { 
					
					id:group.player.@url.toString().split( '?v=' )[1],
					url:group.player.@url.toString(),
					description:entry.content.toString(), 
					thumbs:thumbs
					
				} ); 
				
			}			
			
			dispatchEvent( new YouTubeEvent( YouTubeEvent.FEED_COMPLETE ) );
			
		}
		
		
		public function get arrVideos():Array {
			
			return _arrVideos;
			
		}
		
		public function set defaultUser( user:String ) {
			
			_defaultUser = user;
			
		}
		
	}
}


/*
 * OLD METHOD --- 
 * 
var loader:Loader = new Loader();

function loadVideo( id:String ):void {
	
	loader.contentLoaderInfo.addEventListener( Event.INIT, loadVideoHandler );
	loader.load( new URLRequest( "http://www.youtube.com/v/" + id ) );
	
}

function loadVideoHandler(event:Event):void {
	
	var urlVars:URLVariables = new URLVariables();
	var arrParams:Array = new Array();
	var videoObj:Object = new Object();
	
	//trace( unescape( loader.contentLoaderInfo.url ) );
	
	arrParams = unescape( loader.contentLoaderInfo.url ).split('?')[1].split( '&' );
		
	for( var i:uint = 0; i < arrParams.length; i++ ){
		
		videoObj[ arrParams[i].split('=')[0] ] = arrParams[i].split('=')[1];
				
	}
	
	trace( 'swf=> '+videoObj.swf );
	trace( 'video_id=> '+videoObj.video_id );
	trace( 'rel=> '+videoObj.rel );
	trace( 'showsearch=> '+videoObj.showsearch );
	trace( 'eurl=> '+videoObj.eurl );
	trace( 'iurl=> '+videoObj.iurl );
	trace( 'use_get_video_info=> '+videoObj.use_get_video_info );
	trace( 'load_modules=> '+videoObj.load_modules );
	trace( 'hqt=> '+videoObj.hqt );
	
	trace( '-------------------------------' );
	
	loader.unload();
	
}

loadVideo( 'Wxd9LEpS74I' );
*/